package com.runa.rccprotocol.analysis;

import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.protocol.util.FaultUtil;
import com.runa.protocol.analysis.ShellBufferAnalysis;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.runa.RunaConcenAffirmDeny;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 飞控NB设备主动上报解析器
 */
public class FKNBAnalysis extends ShellBufferAnalysis<byte[]> {

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

    public FKNBAnalysis() {
        super(null, 0);
    }

    @Override
    public int fill(byte[] data, int length,String n) {
        int begin;
        for (begin = 0;begin < length;begin++) {
            if ((data[begin]&0xff) == 0x69
                    && data[begin+3] == 0x69) {
                break;
            }
        }
        int L = data[begin + 1] & 0xff;
        if (length < L + 6) return -1;
        if (data[begin + 4 + L] == ByteUtil.sum(data, begin+4, L)) {
            if (data[begin + 4] == 8) {
                if (data[begin + 6] == (byte) 0x72) {
                    valveData(begin + 5, data);
                } else if (data[begin + 6] == (byte) 0x73 && data[begin + 7] == (byte) 0x0f) {
                    String addr = ByteUtil.byteToHexStringFlip(data, begin+8, 4);
                    netEquNo = addr;
                    this.interData = new RunaConcenAffirmDeny(addr, true);
                }
            }
        }
        return length - L - 6;
    }

    private void valveData(int index, byte[] data) {
        RunaConcenResponseData response = new RunaConcenResponseData();
        response.setUpward(true);
        ValveVO valveData = new ValveVO();
        valveData.setIndex((int) data[index]);
        index += 2;
        valveData.setValveSerial(ByteUtil.byteToHexStringFlip(data, index, 4));
        //网络设备号必须与阀号相同
        response.setAddress(valveData.getValveSerial());
        index += 4;
        String factory = ByteUtil.byteToHexStringFlip(data, index, 2);
        index += 2;
        valveData.setSoftVersion(Integer.toHexString(data[index++]));
        int type = data[index++];
        int access = data[index++];
        index += 4;
        String protoVersion =  ByteUtil.byteToHexStringFlip(data, index, 2);
        index += 7;
        int alarm = data[index++] & 0xff;
        valveData.setFaultCode(FaultUtil.fkNbValveFaults(alarm));
        valveData.setStatus(Integer.toHexString(alarm));
        index ++;
        valveData.setOpenLock(data[index] != 0);
        index += 3;
        valveData.setActOpen((float) (ByteUtil.bcdToInt(data, index, 2, false) * 0.1));
        index += 2;
        int model = data[index++];
        valveData.setFkModel(model);
        int openUp = data[index++];
        int openDown = data[index++];
        //todo 没有确定的方法就先不给le
//        valveData.setValveLockTarget((float) (ByteUtil.bcdToInt(data, index, 2, false) * 0.1));
        valveData.setRemoteSet((float) (ByteUtil.bcdToInt(data, index, 2, false) * 0.1));
        index += 5;
        valveData.setTemperatureLockTarget((float) (ByteUtil.bcdToInt(data, index, 2, false) * 0.1));
        index += 3;
        valveData.setSupplyTemp((float) (ByteUtil.bcdToInt(data, index, 2, false) * 0.1));
        index += 2;
        valveData.setReturnTemp((float) (ByteUtil.bcdToInt(data, index, 2, false) * 0.1));
        index += 20;
        int interval = ByteUtil.bcdToInt(data, index, 2, false);
        valveData.setStandby7(interval);
        index += 2;
        try {
            valveData.setReadTime(new Date());
            valveData.setValveDate(sdf.parse(ByteUtil.bcd2str(data, index, 7, false)));
        } catch (ParseException e) {
            
        }
        index += 7;
        int period = ByteUtil.bcdToInt(data, index, 2, false);
        valveData.setReportPeriod(period);
        index += 2;
        String ip = (data[index++] & 0xff) + "." + (data[index++] & 0xff) +
                "." + (data[index++] & 0xff) + "." + (data[index++] & 0xff);
        index += 2;
        int port = ByteUtil.byteToUShort(data, index, ByteOrder.AB);
        valveData.setIpv4(ip);
        valveData.setIpPort(port);
        response.addRepData(valveData);
        interData = response;
    }

    public static void main(String[] args) {
        byte[] bytes = ByteUtil.hexStringToByte("6966666908C5729761943601100112000000000F1801F10000000040000000000010030000001000000065030000008601000000000000000000000000000000259900000625011723062120100004210421000000000500000000170000000035000000002C00000000D216");
        FKNBAnalysis analysis = new FKNBAnalysis();
        analysis.fill(bytes, bytes.length,"");
        analysis.gain();
    }
}
